/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from "antd";
import { isLegalData } from "@/utils/utils";
import {
  getbrandList,
  getPostSetList,
  addPostSet,
  delPostSet,
  getNoticeList,
  delNoticeList,
  saveNotice,
  editNotice,
  getImgURL,
} from "../services/api";

export default {
  namespace: "setup",

  state: {
    list: [],
    pagination: null,
    postSetList: [],
    noticeList: {
      list: [],
    },
    firstPageList: {
      list: [],
    },
  },

  effects: {
    *getbrandList({ payload, then }, { call, put }) {
      const response = yield call(getbrandList, payload);
      if (!response) return;
      const { data } = response;
      let listData = [];
      // 判断data返回格式
      if (Object.keys(data).length > 0) {
        listData = data;
      } else if (JSON.stringify(data).length == 4) {
        listData = [];
      } else if (Object.keys(data).length == 0) {
        listData = [];
      } else {
        listData = [];
      }
      yield put({
        type: "save",
        payload: {
          list: listData,
        },
      });
      then(data);
    },
    // 设置管理，报表数据推送设置，获取列表
    *getPostSetList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getPostSetList, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            postSetList: isLegalData(data, []),
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 设置管理，报表数据推送设置，添加
    *addPostSet({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(addPostSet, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        message.success(msg);
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 设置管理，报表数据推送设置，删除
    *delPostSet({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(delPostSet, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        message.success(msg);
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 设置管理，通知管理，获取列表
    *getNoticeList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getNoticeList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      const { page, list, total } = data

      if (code == 0) {
        if( page == 1 ){
          yield put({
            type: "save",
            payload: {
              firstPageList: {
                list: isLegalData(list, []),
              },
            },
          });
        }
        yield put({
          type: "save",
          payload: {
            noticeList: {
              list: isLegalData(list, []),
              page: Number(page),
              total,
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 设置管理，通知管理，删除列表
    *delNoticeList({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(delNoticeList, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        message.success(msg);
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 设置管理，通知管理，保存通知（提交审核）
    *saveNotice({ payload, __dva_resolve, __dva_reject }, { call }) {
      const { status } = payload
      const response = yield call(saveNotice, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        if( status == '1'){
          message.success("保存草稿成功");
        }else{
          message.success("已提交审核")
        }
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 设置管理，通知管理，保存通知（提交审核）
    *editNotice({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(editNotice, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        message.success(msg);
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 设置管理，通知管理，获取上传图片URL
    *getImgURL({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getImgURL, payload);
      if (!response) return;
      const { code, msg, data } = response;
      if (code == 0) {
        __dva_resolve(data);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
